<template>
  <div class="app-container">
    <div class="filter-container">
      <label style="color:#909399;font-weight:500;">线路编号：</label>
      <el-input
        v-model="queryParams.typeNumber"
        placeholder="请输入类型编号"
        style="width: 300px;"
        class="filter-item search-item"
        clearable
      />
      <label style="color:#909399;font-weight:500;">线路名称：</label>
      <el-input
        v-model="queryParams.name"
        placeholder="请输入类型名称"
        style="width: 300px;"
        class="filter-item search-item"
        clearable
      />
      <label style="color:#909399;font-weight:500;">线路类型：</label>
      <!-- <template>
        <el-select v-model="value" clearable placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
      </template> -->
      <el-button
        style="background-color: #E05635;color: #fff;border-radius: 5px;border-color: #DCDFE6;"
        @click="search"
      >搜索</el-button>
      <el-button
        style="background-color: #fff;color: #606266;border-radius: 5px;border-color: #DCDFE6;"
        @click="reset"
      >重置</el-button>
    </div>
    <el-card shadow="never" style="margin-top: 10px;">
      <el-button
        style="background-color: #E05635;color: #fff;border-radius: 5px;border-color: #DCDFE6;"
        @click="add"
      >+新增线路</el-button>

      <el-table
        :key="tableKey"
        ref="table"
        v-loading="loading"
        row-key="id"
        lazy
        :load="load"
        :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
        :data="tableData.items"
        :header-cell-style="{background:'#FCFBFF',border:'0'}"
        fit
        style="width: 100%;"
        @row-click="clickTable" 
        @selection-change="onSelectChange"
        @sort-change="sortChange"
      >
        <el-table-column label="线路编号" align="center" prop="lineNumber" width="200px">
        </el-table-column>

        <el-table-column
          label="线路名称"
          :show-overflow-tooltip="true"
          align="center"
          prop="name"
        >
        </el-table-column>
        <el-table-column
          label="线路类型名称"
          :show-overflow-tooltip="true"
          align="center"
          prop="transportLineType.name"
        >



        </el-table-column>

        <el-table-column
          label="所属机构id"
          :show-overflow-tooltip="true"
          align="center"
          prop="agency.name"
        >
        </el-table-column>



        <el-table-column
          label="起始地机构"
          :show-overflow-tooltip="true"
          align="center"
          prop="startAgency.name"
        >
        </el-table-column>

        <el-table-column
          label="目的地机构"
          :show-overflow-tooltip="true"
          align="center"
          prop="endAgency.name"
        >
         
        </el-table-column>

        <el-table-column
          label="距离(千米)"
          align="center"
          prop="distance"
        > 
        </el-table-column>

        <el-table-column
          label="成本(元)"
          align="center"
          prop="cost"
        > 
        </el-table-column>

        <el-table-column
          label="预计时间"
          align="center"
          prop="estimatedTime"
        > 
        </el-table-column>    
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
          width="150px"
        >
          <template slot-scope="{row}">
            <i style="color: #009EFF;" @click="addTrip()">添加车次</i>
            <el-divider direction="vertical"></el-divider>
            <i style="color: #009EFF;" @click="view(row)">地图</i>
            <el-divider direction="vertical"></el-divider>
            <i style="color: #009EFF;" @click="edit(row)">修改</i>
            <el-divider direction="vertical"></el-divider>
            <i
              style="color: #E05635;"
              @click="del(row)"
            >删除</i>

          </template>

              </el-table-column>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-table
              :header-cell-style="{background:'#FCFBFF',border:'0'}"
              :data="childtable"
              style="width: 100%"
            >
              <el-table-column
                label="车次编号"
                width="180"
                prop="id"
              >
              </el-table-column>
              <el-table-column
                prop="name"
                label="车次名称"
                width="180"
              >
              </el-table-column>

              <el-table-column
                prop="period"
                label="发车频次"
                width="180"
              >
              </el-table-column>

              <el-table-column
                prop="arrivalTime"
                label="到达时间"
                width="180"
              >
                <span>{{ props.row.arrivalTime }}</span>
              </el-table-column>

              <el-table-column
                label="所选车辆和司机"
                width="180"
              >
                <template slot-scope="scope">
                  <span v-for="list in scope.row.truckDrivers">{{ list.truck.name }},{{ list.driver.name }}</span>
                </template>
                
              </el-table-column>
      
              <el-table-column
                label="操作"
                align="center"
                class-name="small-padding fixed-width"
                width="150px"
              >
                <template slot-scope="{row}">
                  <i style="color: #009EFF;" @click="edittrips(row)">编辑车次</i>
                  <el-divider direction="vertical"></el-divider>
                  <i
                    style="color: #E05635;"
                    @click="del(row)"
                  >删除车次</i>
                  <i style="color: #009EFF;" @click="map(row)">安排司机</i>
                  <el-divider direction="vertical"></el-divider>
                  <i style="color: #009EFF;" @click="edit(row)">安排车辆</i>
                  <el-divider direction="vertical"></el-divider>

                </template>
              </el-table-column>
            </el-table>
          </template>
        </el-table-column>

      </el-table>
      <el-pagination
        v-show="tableData.counts>0"
        :limit.sync="pagination.size"
        :page.sync="pagination.current"
        :total="Number(tableData.counts)"
        @pagination="fetch"
      />

      <user-edit
        ref="edit"
        :dialog-visible="dialog.isVisible"
        :type="dialog.type"
        @close="editClose"
        @success="editSuccess"
      />


      <user-view ref="view" :dialog-visible="userViewVisible" @close="viewClose" />

      
      <user-trip
        ref="edit"
        :dialog-visible="TripVisible"
        :type="dialog.type"
        @close="editClose"
        @success="editSuccess"
      />
  
    
    </el-card></div>
</template>

<script>
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import UserEdit from './Edit'
import UserView from './View'
import transportLine from '@/api/transportLine.js'


export default {
  components: { UserEdit, UserView },
  filters: {
    sexFilter(status) {
      const map = {
        W: 'success',
        M: 'danger',
        N: 'info'
      }
      return map[status] || 'info'
    },
    statusFilter(status) {
      const map = {
        false: 'danger',
        true: 'success'
      }
      return map[status] || 'success'
    }
  },
  data() {
    return {
      orgList: [],
      dialog: {
        isVisible: false,
        type: 'add'
      },
      userViewVisible: false,
      tableKey: 0,
      queryParams: {},
      sort: {},
      selection: [],
      // 以下已修改
      loading: false,
      tableData: {
        total: 0
      },
      pagination: {
        pagesize: 10,
        page: 1
      }
    }
  },
  computed: {
    currentUser() {
      return this.$store.state.account.user
    }
  },
  watch: {
    // $route() {
    //   if (this.$route.path === '/user/user') {
    //     this.initOrg()
    //   }
    // }
  },
  mounted() {
    this.initOrg()
    this.fetch()
    
  },
  methods: {
     handleCurrentChange(val) {
    this.page = val;
  },
    initOrg() {
      transportLine.allTree().then(response => {
        const res = response.data
        this.orgListXL = res
      })
    },
    myAvatar(avatar) {
      if (!avatar) {
        return require(`@/assets/avatar/default.jpg`)
      } else {
        if (avatar.startsWith('http://') || avatar.startsWith('https://')) {
          return avatar
        } else {
          return require(`@/assets/avatar/${avatar}`)
        }
      }
    },
    filterStatus(value, row) {
      return row.status === value
    },
    viewClose() {
      this.userViewVisible = false
    },
    editClose() {
      this.dialog.isVisible = false
    },
    editSuccess() {
      this.search()
    },
    onSelectChange(selection) {
      this.selection = selection
    },
    loadListOptions({ callback }) {
      callback()
    },
    search() {
      this.fetch({
        ...this.queryParams,
        ...this.sort
      })
      this.initOrg()
    },
    reset() {
      this.queryParams = {}
      this.sort = {}
      this.$refs.table.clearSort()
      this.$refs.table.clearFilter()
      this.search()
    },
    exportExcel() {
      this.$message({
        message: '待完善',
        type: 'warning'
      })
    },
    resetPassword() {
      if (!this.selection.length) {
        this.$message({
          message: this.$t('tips.noDataSelected'),
          type: 'warning'
        })
        return
      }
      this.$confirm(
        this.$t('tips.confirmRestPassword'),
        this.$t('common.tips'),
        {
          confirmButtonText: this.$t('common.confirm'),
          cancelButtonText: this.$t('common.cancel'),
          type: 'warning'
        }
      )
        .then(() => {
          const ids = []
          this.selection.forEach(u => {
            ids.push(u.id)
          })
        })
        .catch(() => {
          this.clearSelections()
        })
    },
    singleDelete(row) {
      this.batchDelete(row)
    },
    batchDelete(row) {
      
     this.$confirm(`此操作将删除${row.name} , 是否继续?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
            this.delete(row.id)
        })
        .catch(() => {
          this.clearSelections()
        })
    },
    clearSelections() {
      this.$refs.table.clearSelection()
    },
    delete(id) {
      console.log(id)
      transportLine.delete({id:id}).then(response => {
        const res = response.data
        if (res.isSuccess) {
          this.$message({
            message: this.$t('删除成功'),
            type: 'success'
          })
        }
        this.search()
      })
    },
    add() {
      this.dialog.type = 'add'
      this.dialog.isVisible = true
      this.$refs.edit.setUser(false, this.orgList,this.orgListXL)
    },
    view(row) {
      this.$refs.view.setUser(row, this.orgList)
      this.userViewVisible = true
    },
    edit(row) {
      this.$refs.edit.setUser(row, this.orgList)
      this.dialog.type = 'edit'
      this.dialog.isVisible = true
    },
    fetch(params = {}) {
      this.loading = true
     
      params.pageSize = this.pagination.pagesize
      params.page = this.pagination.page
      transportLine.page(params).then(response => {
        const res = response.data
        this.loading = false
        this.tableData = res 
        console.log(res)
      })
    },
    sortChange(val) {
      this.sort.field = val.prop
      this.sort.order = val.order
      this.search()
    }
  }
}
</script>
<style lang="scss">
.el-tag.el-tag--danger {
  background-color: #009eff;
  color: #fff;
  border-radius: 30px;
  border-color: #009eff;
}
.el-tag.el-tag--success {
  background-color: #e05635;
  color: #fff;
  border-radius: 30px;
  border-color: #e05635;
}
.el-table {
  border: 1px solid #f7f6f9;
}
.el-table tr,
.el-table td {
  border-top: 0;
  border-right: 0;
  border-bottom: 1px solid #f7f6f9;
}
</style>
